# -*- coding: UTF-8 -*-
# __author__ = 'Sengo'
import cv2 as cv
from settings import BASE_DIR
import numpy as np
from matplotlib import pyplot as plt


def read_photo(name):
    img = cv.imread(BASE_DIR+"/data/%s.jpg" % name, 0)
    return img


def show_photo(img, name="img"):
    cv.imshow(name, img)
    cv.waitKey(0)
    cv.destroyAllWindows()


def save_photo(img, name):
    path = BASE_DIR + '/%s.jpg' % name
    cv.imwrite(path, img)


def split_3_img(img):
    h, w = img.shape
    # Create a blank image of one third height of the original image but with same width
    h = h / 3
    img_blue = img[0:h, 0:w]
    img_green = img[h:2*h, 0:w]
    img_red = img[2*h:3*h, 0:w]

    # show_photo(img_blue, "img_blue")
    # show_photo(img_green, "img_green")
    # show_photo(img_red, "img_red")
    return img_blue, img_green, img_red


def candy_img(img, min=100, max=150):
    return cv.Canny(img, min, max)


def show_3_photo(img1, img2, img3, name1="b", name2="g", name3="r"):
    plt.subplot(131), plt.imshow(img1, cmap='gray')
    plt.title('b'), plt.xticks([]), plt.yticks([])
    plt.subplot(132), plt.imshow(img2, cmap='gray')
    plt.title('g'), plt.xticks([]), plt.yticks([])
    plt.subplot(133), plt.imshow(img3, cmap='gray')
    plt.title('r'), plt.xticks([]), plt.yticks([])
    plt.show()


def spilit_top_bottom_left_right(img):
    """split the 10% top bottom left right"""
    h, w = img.shape
    top = img[0:int(h/10.0), 0:w]
    bottom = img[int(9/10.0*h):h, 0:w]
    left = img[0:h, 0:int(w/10.0)]
    right = img[0:h, int(9.0/10*w):w]
    return top, bottom, left, right


def line_detect_whole_pic(canny, minLineLength, maxLineGap=10):
    lines = cv.HoughLinesP(canny, 1, np.pi / 180, minLineLength, maxLineGap)
    return lines


def lines_detect(img, minLineLength, maxLineGap=10):
    """input canny-img or gray-img"""

    h, w = img.shape
    a = int(h / 10.0)
    top = img[0:a, 0:w]
    b = int(9 / 10.0 * h)
    bottom = img[b:h, 0:w]
    c = int(w / 10.0)
    left = img[0:h, 0:c]
    d = int(9.0 / 10 * w)
    right = img[0:h, d:w]

    # 检测top百分10部分， 拿最下面那条线的y
    lines = line_detect_whole_pic(top, minLineLength, maxLineGap)
    delete_top_h = 0
    if lines.any():
            delete_top_h = find_max_value(lines, -1)
    # 检测底部百分10， 拿最上面那条线的y
    delete_btm_h = 0
    lines = line_detect_whole_pic(bottom, minLineLength, maxLineGap)
    if lines.any():
            delete_btm_h = find_min_value(lines, -1)
            delete_btm_h += b

    # 检测左部百分10， 拿最右面那条线的x
    delete_l_x = 0
    lines = line_detect_whole_pic(left, minLineLength, maxLineGap)
    if lines.any():
        delete_l_x = find_max_value(lines, 0)

    # 检测右部百分10， 拿最左面那条线的x
    delete_r_x = 0
    lines = line_detect_whole_pic(right, minLineLength, maxLineGap)
    if lines.any():
        delete_r_x = find_min_value(lines, 0)
        delete_r_x += d

    return delete_top_h, delete_btm_h, delete_l_x, delete_r_x


def find_max_value(l, index):
    """查找[[[x1,y1, x2, y2]..]]中里面第index个值的最大值"""
    re = []
    for i in l:
        for j in i:
            re.append(j[index])
    return max(re)


def find_min_value(l, index):
    """查找[[[x1,y1, x2, y2]..]]中里面第index个值的最小值"""
    re = []
    for i in l:
        for j in i:
            re.append(j[index])
    return min(re)


def resize_photo(img, facotr=0.5):
    """缩小图片 factore为缩小比例 要小于1"""
    res = cv.resize(img, None, fx=facotr, fy=facotr, interpolation=cv.INTER_AREA)
    return res


def pyramid(img):
    """缩小到x 小于等于500"""
    re = img[:]
    # i是缩放次数
    i = 0
    while 1:
        if re.shape[0] <= 500:
            return re, i
        i += 1
        re = resize_photo(re)


if __name__ == '__main__':
    pic1 = "00056v"
    pic2 = "10131v"
    pic3 = "00458u"  # big file
    img1 = read_photo(pic3)
    b, g, r = split_3_img(img1)
    b, i = pyramid(b)
    print b.shape, i
    cv.imshow("b", b)
    cv.waitKey(0)

